﻿using System;
using System.Data;
using System.IO;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using IronOne.SalesOne.DataObjects.QuotationReportDSTableAdapters;

namespace IronOne.SalesOne.SalesOneWeb.Collection
{
    public partial class ViewReportCollection : System.Web.UI.Page
    {
        //int branchCode;
        DateTime DateFrom;
        DateTime DateTo;

        protected void Page_Load(object sender, EventArgs e)
        {
            string[] brCodes = Page.Request.Params["branchCode"].ToString().Split('.');
            int[] branchCodes = new int[brCodes.Length];
            for (int i = 0; i < branchCodes.Length - 1; i++)
            {
                branchCodes[i] = int.Parse(brCodes[i]);
            }

            //branchCode = Convert.ToInt32(Page.Request.Params["branchCode"].ToString());
            //string sdf = Page.Request.Params["FromDate"].ToString();
            //sdf = Page.Request.Params["ToDate"].ToString();

            DateFrom = Convert.ToDateTime(Page.Request.Params["FromDate"].ToString());
            DateTo = Convert.ToDateTime(Page.Request.Params["ToDate"].ToString());

            ReportDocument CollReport = new ReportDocument();
            string reportPath = Server.MapPath("RptCollectionOld/RptCollection.rpt");
            CollReport.Load(reportPath);

            CollReportTableAdapter ColAdp = new CollReportTableAdapter();
            DataTable dt = ColAdp.GetDataByCollection(DateFrom, DateTo, branchCodes[0]);
            CollReport.SetDataSource(dt);

            CrystalReportViewer1.ReportSource = CollReport;

            MemoryStream oStream; // using System.IO
            oStream = (MemoryStream)
            CollReport.ExportToStream(ExportFormatType.PortableDocFormat);

            ////CrystalDecisions.Shared.DiskFileDestinationOptions DiskOpts = new CrystalDecisions.Shared.DiskFileDestinationOptions();

            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/pdf";
            Response.BinaryWrite(oStream.ToArray());
            Response.End();
        }
    }
}
